package Logs;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.text.SimpleDateFormat;


import System.Classifier;

/**
 * Description of the class LogFile.
 * this class is implements  Log when it's saved on file 
 * @author Shinichi
 *
 */
public class LogFile implements Log {
	public Classifier myClassifier;// classifier tells log -every time that there is changes in the tags of a file . 
	public File file;// a file handler 
	protected final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	public LogFile(Classifier a) {
		myClassifier=a;
		if(file==null){
			file=new File("Log.txt");
			file.deleteOnExit();
			try {
				FileWriter fW=new FileWriter(file,true);
			} catch (IOException e) {}
		}
	}

	/**
	 * Description of the method update.
	 * what append when we need to update the logFile
	 */
	public void update(String message) {
		try {
			BufferedWriter writer= new BufferedWriter(new FileWriter(file,true));
			writer.write(now()+" "+message);
			writer.newLine();
			writer.flush();
			writer.close();
		} catch (IOException e) {}
	}

	/**
	 * Description of the method open.
	 * what to do when we open the logFile
	 */
	public void Open() {
	}

	/**
	 * @return
	 */
	protected String now() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		return sdf.format(cal.getTime());
	}

	/**
	 * @see Logs.Log#fileme()
	 */
	public File fileme() {
		if(file==null){
			file=new File("Log.txt");
			file.deleteOnExit();
		}
		return file;
	}


}